package com.pupil.down.util.protocol;

import com.pupil.down.util.LogUtil;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.Base64;

/**
 * @Description: 迅雷链接转换工具
 * @Author: pupil
 * @Date: 2024/10/01 下午 8:37
 */
public class ThunderUtil implements ProtocolUtil {

    public static String PREFIX = "thunder://";

    @Override
    public String toHttpUrl(String url) {
        LogUtil.info("当前链接是迅雷链接，开始转换...");
        url = url.replace(PREFIX, "");
        try {
            // base 64 转换
            url = new String(Base64.getDecoder().decode(url.getBytes(StandardCharsets.UTF_8)));
            // url 解码
            url = URLDecoder.decode(url,"UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        // 去头去尾
        if (url.startsWith("AA")) {
            url = url.substring(2);
        }
        if (url.endsWith("ZZ")) {
            url = url.substring(0, url.length() - 2);
        }
        LogUtil.info("当前链接是迅雷链接，转换结果:{}", url);

        return url;
    }
}
